<div class="listing_button">
	<?=form_input(array('type' => 'text', 'name' => 'term', 'id' => 'search_box', 'placeholder' => 'item name'))?>
	<?=anchor('inventory/add_edit_item', 'New Item', 'id="new_item_button"')?>
	<?=anchor('sales/cancel_sale/', 'Clear', 'id="clear_items_button"');?>
</div>
<div id="sale_overview">
	<?$this->load->view('admin/includes/_sales_summary')?>
</div>
<div id="new_item_modal"></div>
<script>
$(function(){
	$('#new_item_button').button({icons:{primary:'ui-icon-plusthick'}});
	$('#clear_items_button').button({icons:{primary:'ui-icon-trash'}});

	$('input#search_box').autocomplete({
		source: function(request, response) {
			$.ajax({
				type: 'post',
				url: 'inventory/item_autocomplete/',
				data: {
					term: request.term
				},
				dataType: 'json',
				success: response
			});
		},
		select: function( event, ui ) {
			$.get('sales/add_item_cart/'+ui.item.id, function() {
				$('#sale_overview').load('sales/sales_table_update/');
				$('#search_box').val('');
			});
		}
	});

	$('#clear_items_button').click(function(e){
		e.preventDefault();
		$.get($(this).attr('href'), function() {
			$('#sale_overview').load('sales/sales_table_update/');
		});
	});

	$('#checkout_items_button').livequery('click',function(e){
		e.preventDefault();
		$.ajax({
			url: 'sales/complete_sale/',
			type: 'post',
			data: {
				comment: $('#sales_comment').val()
			},
			success: function(response) {
				var pop = confirm('Do you want to print a recipt?');
				if(pop) {
					$("#sales_summary").jqprint();
				}
				$('#sale_overview').load('sales/sales_table_update/');
			}
		});
	});

	$('#new_item_button').click(function(e){
		e.preventDefault();
		$('#new_item_modal').load($(this).attr('href'), function() {
			$(this).dialog('open');
		});
	});

	$("#new_item_modal").dialog({
		autoOpen: false,
		modal: true,
		title: 'New Item',
		minWidth: 350,
		resizable:false,
		buttons: {
			Add: function() {
				$("#new_item_modal form").submit();
			},
			Cancel: function(){
				$(this).dialog("close");
			}
		}
	});
});
</script>